package com.lanou3g.mynewstest.module.news.headline;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou3g.mynewstest.R;
import com.lanou3g.mynewstest.base.MyBaseActivity;
import com.lanou3g.mynewstest.beans.NewsHeadlineSpecialBeans;
import com.lanou3g.mynewstest.tools.MyRequestQueue;

import java.util.ArrayList;

/**
 * Created by dllo on 16/1/13.
 */
public class SpecialActivity extends MyBaseActivity {

    private static NewsHeadlineSpecialBeans data;
    private ExpandableListView expandableListView;
    private TextView title, digest, foot, lead;
    private int buttonsID[] = {R.id.item_special_head_btn1, R.id.item_special_head_btn2,
            R.id.item_special_head_btn3, R.id.item_special_head_btn4,
            R.id.item_special_head_btn5, R.id.item_special_head_btn6,
            R.id.item_special_head_btn7, R.id.item_special_head_btn8};
    private ArrayList<Button> buttons;
    private Button btn;
    private SpecialAdapter adapter;
    private NetworkImageView imageView;

    public static void goToSpecialActivity(Context context, NewsHeadlineSpecialBeans data) {
        SpecialActivity.data = data;
        Intent intent = new Intent(context, SpecialActivity.class);
        context.startActivity(intent);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_special);
        expandableListView = (ExpandableListView) findViewById(R.id.special_ex);
        title = (TextView) findViewById(R.id.special_title_tv);
        title.setText(data.getSpecial().getSname());
        adapter = new SpecialAdapter(data, getBaseContext());


        View headView = LayoutInflater.from(getBaseContext()).inflate(R.layout.item_special_head, null);
        imageView = (NetworkImageView) headView.findViewById(R.id.item_special_head_iv);
        MyRequestQueue.setNetImage(imageView, data.getSpecial().getBanner());
        digest = (TextView) headView.findViewById(R.id.item_special_head_digest);
        lead = (TextView) headView.findViewById(R.id.item_special_head_lead);
        if (data.getSpecial().getDigest() != null && data.getSpecial().getDigest().length() > 0) {
            digest.setText("         " + data.getSpecial().getDigest());
            digest.setVisibility(View.VISIBLE);
            lead.setVisibility(View.VISIBLE);
        } else {
            digest.setVisibility(View.INVISIBLE);
            lead.setVisibility(View.INVISIBLE);
        }

        buttons = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            btn = (Button) headView.findViewById(buttonsID[i]);
            btn.setVisibility(View.INVISIBLE);
            buttons.add(btn);
        }
        for (int i = 0; i < data.getSpecial().getTopics().size(); i++) {
            if (i == 8) {
                break;
            }
            buttons.get(i).setText(data.getSpecial().getTopics().get(i).getShortname());
            buttons.get(i).setVisibility(View.VISIBLE);

        }

        expandableListView.addHeaderView(headView);

        View footView = LayoutInflater.from(getBaseContext()).inflate(R.layout.item_special_foot, null);
        foot = (TextView) footView.findViewById(R.id.item_special_foot_tv);
        foot.setText("责任编辑: " + data.getSpecial().getEc());
        expandableListView.addFooterView(footView);

        expandableListView.setAdapter(adapter);

        for (int i = 0; i < adapter.getGroupCount(); i++) {
            expandableListView.expandGroup(i);
        }
        //隐藏箭头
        expandableListView.setGroupIndicator(null);
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

                return true;
            }
        });
        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
            }
        });

    }
}
